function ...
mappedSignal = WiSiL_Precoder_layerMapper( modulatedSignalTx, transmitter )
% WISIL_PRECODER_LAYERMAPPER Map bits into MIMO layers 
%
%   MAPPED_SIGNAL WiSiL_Precoder_layerMapper( SIGNALTX, TRANSMITTER ) maps
%   the modulation symbols from SIGNALTX into the layers in MAPPES_SIGNAL,
%   according to the parameters in TRANSMITTER.
%   SIGNALTX is a N-element column vector.
%   TRANSMIITER must have the following fields:
%              .layers - A scalar representing the number of layers in
%                        which mapping will be done.
%              .scheme - a string wth one of the following values: 'none', 
%                        'diversity' or 'multiplexing'
%   MAPPED_SIGNAL is a N'xV matrix, in which N' represents the number of
%   symbols in each layer and V represents the number of layers.
%
% See 3GPP TS 36.211 V8.2.0 for more information.

% History:
% Rafhael Amorim 12/02/2008
%  created

scheme = transmitter.scheme; 
layers = transmitter.layers; 

nsymb = length(modulatedSignalTx);

if(strcmpi(scheme,{'none'})==1) 
    %for one antenna no precoding is performed.
    mappedSignal = modulatedSignalTx; 
end

if(strcmpi(scheme,{'diversity'})==1)

    if layers == 2 
        mappedSignal = ( reshape( modulatedSignalTx, 2, ...
                         nsymb/2 ) );
    
    elseif layers == 4 
        mappedSignal = ( reshape(modulatedSignalTx, 4, ...
                         nsymb/4 ) );
    else
        error('Invalid number of transmission layers for the diversity scheme: Choose two or four layers!')
    end

end

if (strcmpi(scheme,{'multiplexing'})==1)    
    
    if layers == 1
         % For 1 layer the mapped signal is equal to the input signal.
        mappedSignal(1,:) = modulatedSignalTx;

    elseif layers == 2
        % Layer Mapping performed according 3GPP TS 36.211 V8.2.0
        codeWord0(1,:) =  modulatedSignalTx (1:(nsymb)/2);
        codeWord1(1,:) =  modulatedSignalTx ((nsymb)/2 + 1:end);
        mappedSignal = [codeWord0 ; codeWord1];

    elseif layers == 3
        % Layer Mapping performed according 3GPP TS 36.211 V8.2.0
        codeWord0(1,:) =  modulatedSignalTx (1:(nsymb)/3);
        codeWord1(1,:) =  modulatedSignalTx ((nsymb)/3 + 1:end);
        codeWord1 = (reshape(codeWord1,2,(length(codeWord1))/2));
        mappedSignal = [codeWord0 ; codeWord1];
    
    elseif layers == 4
        % Layer Mapping performed according 3GPP TS 36.211 V8.2.0
        codeWord0(1,:) =  modulatedSignalTx (1:(nsymb)/2);
        codeWord1(1,:) =  modulatedSignalTx ((nsymb)/2 + 1:end);
        codeWord0 = (reshape(codeWord0,2,(length(codeWord0))/2));
        codeWord1 = (reshape(codeWord1,2,(length(codeWord1))/2));
        mappedSignal = [codeWord0 ; codeWord1];
    else
        error('Invalid number of transmission layers for the multiplexing scheme: Choose one, two, three or four layers!')
    end
end

end


